package com.hust.controller;

import com.github.pagehelper.PageInfo;
import com.hust.domain.PageResult;
import com.hust.domain.User;
import com.hust.exceptions.ClientException;
import com.hust.service.CommodityService;
import com.hust.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private SubscribeService subscribeService;

    /**
     * @Description: 历史价格查询功能
     * @Param: [pageSize, url] pageSize-所需的最新记录条数 url-商品url
     * @return: com.github.pagehelper.PageInfo
     */
    @RequestMapping("/findHistory")
    @ResponseBody
    public PageInfo findHistroyMsg(@RequestParam(name="pageSize",defaultValue = "5") int pageSize, @RequestParam(name="url",required = true) String url){
        PageInfo historyByUrl = commodityService.findHistoryByUrl(pageSize, url);
        return historyByUrl;
    }

    /*
        订阅功能的实现：
        前端实现一个点击收藏按钮，然后传给后端用户的相关信息，u_id c_id 当前的价格price
        建表subscribe 每次点击收藏发请求插入一条收藏数据，不可重复收藏，前台将收藏设置为灰色也就是说
        每次登陆后查看商品需要发一次请求
        订阅后每隔十秒扫描所有的订阅信息，每扫描一条订阅信息，然后取出cid，根据cid去price表中查询最新价格，
        与当前订阅的价格进行比较，如果跌幅超过20%，则发送邮件，告知降价了多少，邮件中同时包含了商品的url提醒用户
        前去购买

    */

    /**
     * @Description: 订阅功能
     * @Param: [c_id, sub_price]  c_id-对应商品表c_id sub_price-商品订阅时价格
     * @return: void
     */
    @RequestMapping("/subscribe")
    @ResponseBody
    public boolean subscribe(@RequestParam(name="c_id",required = true) Long c_id,
                          @RequestParam(name="sub_price",required = true) String sub_price,
                          HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        return subscribeService.addSubscribe(user.getuId(),user.getuName(),c_id,sub_price);
    }

    /**
    * @Description: 查询是否该user是否订阅了某商品
    * @Param: [c_id, request] c_id-对应商品表c_id
    * @return: boolean
    */
    @RequestMapping("/isSubscribe")
    @ResponseBody
    public boolean isSubscribe(@RequestParam(name="c_id",required = true) Long c_id,
                               HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        return subscribeService.isSubscribe(user.getuId(),c_id);
    }

    @RequestMapping("/top5Hot")
    @ResponseBody
    public PageInfo findTop5Hot(){
        PageInfo top5Hot = commodityService.findTop5Hot();
        return top5Hot;
    }

    //商品搜索
    @RequestMapping("/search")
    @ResponseBody
    public PageResult searchList(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10") Integer limit, String name, String cType){
        PageResult commodityList = null;
        //商品搜索
        try {
            commodityList = commodityService.SearchCommodity(page, limit, name, cType);
        }catch (ClientException e){
            //给前台返回没有此商品的信息

        }
        return commodityList;
    }

    //商品删除
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteCommodity(String id){
      return commodityService.delCommodity(id);
    }

    //商品价格预测
    @RequestMapping("/predict")
    @ResponseBody
    public String predictCommodity(String id){
        return commodityService.preCommodity(id);
    }
}
